package com.jeecg.p3.weixin.web.core;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.p3.core.util.WeiXinHttpUtil;
import org.jeewx.api.core.req.model.message.TemplateData;
import org.jeewx.api.core.req.model.message.TemplateMessageSendResult;
import org.jeewx.api.wxsendmsg.JwSendTemplateMsgAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.jeecg.p3.commonweixin.dao.MyJwWebJwidDao;
import com.jeecg.p3.commonweixin.entity.MyJwWebJwid;
import com.jeecg.p3.system.service.MyJwWebJwidService;
import com.jeecg.p3.weixin.service.WechatService;
import com.jeecg.p3.weixin.util.SignUtil;
import com.jeecg.p3.weixin.vo.SmsVo;

/**
 * 微信客户端，请求处理核心类
 * @author 
 *
 */
@Controller
@RequestMapping("/wechatController")
public class WechatController {
	@Autowired
	private WechatService wechatService;
	@Autowired
	private MyJwWebJwidService webJwidService;
	
	@Autowired
	private MyJwWebJwidDao myJwWebJwidDao;

	@RequestMapping(params="wechat", method = RequestMethod.GET)
	public void wechatGet(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "signature") String signature,
			@RequestParam(value = "timestamp") String timestamp,
			@RequestParam(value = "nonce") String nonce,
			@RequestParam(value = "echostr") String echostr) throws IOException {

		List<MyJwWebJwid> myJwWebJwids = webJwidService.queryAll();
		if(myJwWebJwids != null && myJwWebJwids.size() > 0) {
			for (MyJwWebJwid myJwWebJwid : myJwWebJwids) {
				if(SignUtil.checkSignature(myJwWebJwid.getToken(), signature, timestamp, nonce)) {
					try {
						response.getWriter().print(echostr);
						break;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	@RequestMapping(params = "wechat", method = RequestMethod.POST)
	public void wechatPost(HttpServletResponse response,
			HttpServletRequest request) throws IOException {
		String respMessage = wechatService.coreService(request);
		PrintWriter out = response.getWriter();
		out.print(respMessage);
		out.close();
	}
	
	@RequestMapping(value = "sendsms", method = {RequestMethod.GET,RequestMethod.POST} )
	public void sendsms(HttpServletRequest request,HttpServletResponse response,SmsVo smsVo ) throws Exception {

		String accessToken = WeiXinHttpUtil.getRedisWeixinToken(smsVo.getJwid());
		if(StringUtils.isEmpty(accessToken)) {
			MyJwWebJwid myJwWebJwid = myJwWebJwidDao.queryByJwid(smsVo.getJwid());
			 accessToken = myJwWebJwid.getAccessToken();
		}
			
		TemplateMessageSendResult msgSend =new TemplateMessageSendResult();
		msgSend.setTemplate_id(smsVo.getTemplate_id());
		msgSend.setTouser(smsVo.getTouser());
		msgSend.setUrl("");
		//标题
		Map<String,TemplateData> data = new HashMap();
		TemplateData templateData1 = new TemplateData(); 
		templateData1.setValue("短信提醒");
		data.put("data_1", templateData1);
		
		//短信发送人手机
		TemplateData templateData2 = new TemplateData();
		templateData2.setValue(smsVo.getAddress());
		data.put("data_2", templateData2);
		
		//短信发送人姓名
		TemplateData templateData3 = new TemplateData();
		templateData3.setValue(smsVo.getPerson());
		data.put("data_3", templateData3);
		
/*		//短信接收人手机
		TemplateData templateData4 = new TemplateData();
		templateData4.setValue(" ");
		data.put("data_4", templateData4);*/
		
		//短信接收时间
		TemplateData templateData5 = new TemplateData();
		templateData5.setValue(smsVo.getDate());
		data.put("data_5", templateData5);
		
		//短信内容
		TemplateData templateData6 = new TemplateData();
		templateData6.setColor("#173177");
		templateData6.setValue(smsVo.getBody());
		data.put("data_6", templateData6);
		
		TemplateData templateData7 = new TemplateData();
		templateData7.setValue("");
		data.put("remark", templateData7);
		msgSend.setData(data);
		
		JwSendTemplateMsgAPI jwSendTemplateMsgAPI	= new JwSendTemplateMsgAPI();
		jwSendTemplateMsgAPI.sendTemplateMsg(accessToken, msgSend);
		
		PrintWriter out = response.getWriter();
		out.print("1");
		out.close();
	}
	

}
